import configparser
import os
from functools import lru_cache

config_file = os.path.join(os.path.dirname(__file__), 'config.ini')


class GlobalConfig:
    if not os.path.exists(config_file):
        raise FileNotFoundError("No such file: %s" % config_file)

    _config = configparser.ConfigParser()
    _config.read(config_file, encoding='utf-8')

    # fastapi 配置
    PORT = int(_config.get('fastapi', 'port'))

    # LOG日志配置
    LOG_PATH = _config.get('log', 'LOG_PATH')
    if not os.path.exists(os.path.dirname(LOG_PATH)):
        os.makedirs(os.path.dirname(LOG_PATH))

    LOG_WHEN = _config.get('log', 'LOG_WHEN')
    LOG_BACKCOUNT = int(_config.get('log', 'LOG_BACKCOUNT'))


@lru_cache()
def get_configs():
    return GlobalConfig()


configs = get_configs()
